<!doctype html>
<html>

	<head>
		<meta charset="UTF-8">
		<title></title>
		<meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no" />
		<link href="../../css/mui.min.css" rel="stylesheet" />
		<link href="../../css/mui.indexedlist.css" rel="stylesheet" />
		<style>
			html,
			body {
				height: 100%;
				overflow: hidden;
			}
			.mui-bar {
				-webkit-box-shadow: none;
				box-shadow: none;
			}
		</style>
	</head>

	<body>
		
		<div class="mui-content">
			<div id='list' class="mui-indexed-list">
				<div class="mui-indexed-list-search mui-input-row mui-search">
					<input type="search" class="mui-input-clear mui-indexed-list-search-input" placeholder="搜索好友">
				</div>
				<div class="mui-indexed-list-bar" style="background-color: transparent;">
					<a>A</a>
					<a>B</a>
					<a>C</a>
					<a>D</a>
					<a>E</a>
					<a>F</a>
					<a>G</a>
					<a>H</a>
					<a>I</a>
					<a>J</a>
					<a>K</a>
					<a>L</a>
					<a>M</a>
					<a>N</a>
					<a>O</a>
					<a>P</a>
					<a>Q</a>
					<a>R</a>
					<a>S</a>
					<a>T</a>
					<a>U</a>
					<a>V</a>
					<a>W</a>
					<a>X</a>
					<a>Y</a>
					<a>Z</a>
				</div>
				<div class="mui-indexed-list-alert"></div>
				<div class="mui-indexed-list-inner">
					<div class="mui-indexed-list-empty-alert">没有数据</div>
					<ul id="friend_list" class="mui-table-view">	
					</ul>
				</div>
			</div>
		</div>
		
		<script src="../../js/mui.min.js"></script>
		<script src="../../js/mui.indexedlist.js"></script>
		<script type="text/javascript" src="../../js/app.js" ></script>
		<script type="text/javascript" src="../../js/nickname.js" ></script>
		<script type="text/javascript" charset="utf-8">
			mui.init();
			mui.plusReady(function() {
				//后端获取并储存缓存
				getFriendList();
				
				var thisWebView = plus.webview.currentWebview();
				thisWebView.addEventListener("show", function () {
					console.log("show friend...");
					//好友列表渲染
					renderFriendList();
					//显示通讯录
					showList();
				});
			});
			
			var wordChar = [
				'A', 'B', 'C', 'D', 'E', 'F', 'G',
				'H', 'I', 'J', 'K', 'L', 'M', 'N',
				'O', 'P', 'Q', 'R', 'S', 'T',
				'U', 'V', 'W', 'X', 'Y', 'Z', '#'
			];
			
			var friendArray = [
				[], [], [], [], [], [], [], [], [],
				[], [], [], [], [], [], [], [], [],
				[], [], [], [], [], [], [], [], []
			];
			
			function showList() {
				var list = document.getElementById('list');
				list.style.height = document.body.offsetHeight + 'px';
				window.indexedList = new mui.IndexedList(list);
			}
			
			function getFriendList() {
				
				var userInfo = app.getUserInfo();
				
				mui.ajax(app.serverUrl + '/simple-chat/api/friends/' + userInfo.userId,{
					dataType:'json',
					type:'get',
					timeout:10000,
					headers:{'Authorization':'Bearer ' + app.getToken()},	              
					success:function(data){
						console.log("从后端获取了好友列表")
						var friendList = data.data;
						app.setFriendList(friendList);
					}
				});
			}
			
			function renderFriendList() {
				var friendList = app.getFriendList();
				for(var i = 0; i < friendList.length; i++) {
					var friend = friendList[i];
					var pingyingzhuanhuan = words.convertPinyin(friend.markName);
					var friendChar = pingyingzhuanhuan.substr(0, 1).toUpperCase();
					
					var order = getOrderOfEnWords(friendChar);
					//将信息塞入数组
					friendArray[order].push(friend);
				}
				
				//将好友信息渲染到页面
				var friendHtml = '';
				for(var i = 0; i < friendArray.length; i++) {
					var friendArr = friendArray[i];
					if(friendArr.length > 0) {
						var markNameStartChar = wordChar[i];
						friendHtml += '<li data-group="'+ markNameStartChar +'" class="mui-table-view-divider mui-indexed-list-group">'+ markNameStartChar +'</li>'
						for (var j = 0; j < friendArr.length; j++) {
							friendHtml += '<li friendId="'+ friendArr[j].userId +'" markName="'+ friendArr[j].markName +'" avatar="'+ friendArr[j].avatar +'" class="chat_with_friend mui-table-view-cell mui-indexed-list-item" style="padding: 8px 10px;">' +
											'<img class="mui-media-object mui-pull-left" style="width: 35px;height: 35px;" src="'+ friendArr[j].avatar +'"/>' +
											'<div class="mui-media-body" style="line-height: 35px;">'+ friendArr[j].markName +'</div>' +
										  '</li>';
						}
					}
				}
				
				document.getElementById("friend_list").innerHTML = friendHtml;
				
				//清空好友数组
				clearFriendList();
				
				//跳转聊天页面
				mui("#friend_list").on('tap',".chat_with_friend", function() {
					
					var friendId = this.getAttribute("friendId");
					var friendMarkName = this.getAttribute("markName");
					var friendAvatar = this.getAttribute("avatar");
					
					mui.openWindow({
						url:'../chat/feixin_chat.html', 
						id:"chatting-" + friendId,
						extras:{
							friendId: friendId,
							friendMarkName: friendMarkName,
							friendAvatar: friendAvatar
						}
					});
				});
			}
			
			function getOrderOfEnWords(friendChar){
				for(var i = 0; i < wordChar.length; i++) {
					if(wordChar[i] == friendChar) {
						return i;
					}
				}
				return wordChar.length - 1;
			}
			
			function clearFriendList(){
				friendArray = [
					[], [], [], [], [], [], [], [], [],
					[], [], [], [], [], [], [], [], [],
					[], [], [], [], [], [], [], [], []
				];
			}
		</script>
	</body>

</html>